<?php
namespace Framework\Database;

use PDO;

class Model {

	private $db;

	public $tableName;

	private $attributes;

	public function __construct()
	{
		$this->db = app()->db;
	}

	static public function find()
	{
		return new Query('user');
		// $class = get_called_class();
		// return new $class();
	}

	static public function tableName()
	{
		if( $this->tableName == null ){
			$className = basename(str_replace('\\', '/', get_called_class()));
			return strtolower($className);
		}

		return $this->tableName;
	}


	//从表中仅获取一条记录
	public function one(){
		$data = $this->db->get($this->tableName(), $this->columns, []);
		return $data;

		$_class = get_called_class();
		$class  = new $_class();

		foreach ($data as $key => $value) {
			$class->$key = $value;
		}

		return $class;
	}


	public function __get(string $name){
        $getter = 'get' . $name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        }

        throw new \Exception('获取未知属性 : ' . get_class($this) . '::' . $name);
	}
}